import requests
from collections import Counter

def fetch_submissions(handle, amt=100000):
    url = "https://codeforces.com/api/user.status"
    params = {'handle': handle, 'from': 1, 'count': amt}
    resp = requests.get(url, params=params)
    data = resp.json()
    if data['status'] != 'OK':
        raise RuntimeError(f"API 错误: {data.get('comment')}")
    return data['result']

def get_solved_ratings(handle):
    subs = fetch_submissions(handle)
    seen, ratings = set(), []
    for sub in subs:
        if sub.get('verdict') != 'OK':
            continue
        p = sub['problem']
        key = (p['contestId'], p['index'])
        if key in seen:
            continue
        seen.add(key)
        r = p.get('rating')
        if r: ratings.append(r)
    return ratings

if __name__ == "__main__":
    user = input("请输入 CF 用户名：").strip()
    rts = get_solved_ratings(user)
    cnt = Counter(rts)
    if not cnt:
        print("没有找到已 AC 的有 rating 题目。")
    else:
        print("Rating 分布：")
        for rating in sorted(cnt):
            print(f"  {rating} : {cnt[rating]} 道")
input("执行完毕，按回车键退出…")